package com.dao.Impl;

import com.dao.BankDao;
import com.pojo.Bank;
import com.util.JDBC;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class BankDaoImpl implements BankDao {
    @Override
    public List<Bank> queryBank() {
        String sql = "SELECT * FROM branches WHERE id >?";
        Object[] objects = new Object[1];
        objects[0] = 0;
        ResultSet resultSet = JDBC.query(sql,objects);
        List<Bank> bankList = new ArrayList<Bank>();
        try{
            while (resultSet.next()){
                Bank bank = new Bank();
                bank.setId(resultSet.getInt(1));
                bank.setBusinessNo(resultSet.getString(2));
                bank.setName(resultSet.getString(3));
                bank.setCityArea(resultSet.getString(4));
                bank.setAddress(resultSet.getString(5));
                bank.setTelephone(resultSet.getString(6));
                bank.setSummary(resultSet.getString(7));
                bankList.add(bank);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return bankList;
    }

    @Override
    public int addBank(Bank bank) {
        String sql = "INSERT INTO information.branches (businessNo, name, cityArea, address, telephone, summary)\n" +
                "VALUES (?,?,?,?,?,?);";
        Object[] objects = new Object[6];
        objects[0] = bank.getBusinessNo();
        objects[1] = bank.getName();
        objects[2] = bank.getCityArea();
        objects[3] = bank.getAddress();
        objects[4] = bank.getTelephone();
        objects[5] = bank.getSummary();
        int num = 0;
        num = JDBC.alter(sql,objects);
        return num;
    }

    @Override
    public List<Bank> idQueryBank(Bank bank1) {
        String sql = "SELECT * FROM branches WHERE id = ?";
        Object[] objects = new Object[1];
        objects[0] = bank1.getId();
        ResultSet resultSet = JDBC.query(sql,objects);
        List<Bank> bankList = new ArrayList<Bank>();
        try{
            while (resultSet.next()){
                Bank bank = new Bank();
                bank.setId(resultSet.getInt(1));
                bank.setBusinessNo(resultSet.getString(2));
                bank.setName(resultSet.getString(3));
                bank.setCityArea(resultSet.getString(4));
                bank.setAddress(resultSet.getString(5));
                bank.setTelephone(resultSet.getString(6));
                bank.setSummary(resultSet.getString(7));
                bankList.add(bank);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return bankList;
    }

    @Override
    public List<Bank> noQueryBank(Bank bank1) {
        String sql = "SELECT * FROM branches WHERE businessNo = ?";
        Object[] objects = new Object[1];
        objects[0] = bank1.getBusinessNo();
        ResultSet resultSet = JDBC.query(sql,objects);
        List<Bank> bankList = new ArrayList<Bank>();
        try{
            while (resultSet.next()){
                Bank bank = new Bank();
                bank.setId(resultSet.getInt(1));
                bank.setBusinessNo(resultSet.getString(2));
                bank.setName(resultSet.getString(3));
                bank.setCityArea(resultSet.getString(4));
                bank.setAddress(resultSet.getString(5));
                bank.setTelephone(resultSet.getString(6));
                bank.setSummary(resultSet.getString(7));
                bankList.add(bank);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return bankList;
    }
}
